<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
	<div class="page-header">
		<h1>CMS 栏目管理</h1>
	</div>
	<div class="col-xs-12">
		<form class="form-horizontal" id="category-form">
			<input type="hidden" name="id" value="<?=$categoryInfo['id']?>" />
			<input type="hidden" name="category_article_cover" value="<?=$categoryInfo['category_article_cover']?>" />

			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目名称：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" placeholder="请填写栏目名称" name="category_name" value="<?=$categoryInfo['category_name']?>" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目排序：</label>
				<div class="col-sm-6">
					<input class="form-control" type="text" placeholder="请填写栏目排序" name="category_sort" value="<?=$categoryInfo['category_sort']?>" />
				</div>
			</div>
			<?php if(!$categoryInfo): ?>
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right">上级栏目：</label>
					<div class="col-sm-6">
						<select class="form-control" name="parent_id">
							<option value="0">顶级栏目</option>
							<?php foreach($categoryList as $category): ?>
								<option value="<?=$category['id']?>"><?=$category['category_name']?></option>
							<?php endforeach; ?>
						</select>
					</div>
				</div>
			<?php endif; ?>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目推荐：</label>
				<div class="col-sm-6">
					<select class="form-control" name="category_recommend">
						<option value="0">否</option>
						<option value="1">推荐</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">栏目类型：</label>
				<div class="col-sm-6">
					<select class="form-control" name="category_type">
						<option value="<?=CMS_CATEGORY_NORMAL?>">普通栏目</option>
						<option value="<?=CMS_CATEGORY_ARTICLE?>">文章类型</option>
					</select>
				</div>
			</div>
			<div id="category-article-div" class="hide">
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right">文章封面：</label>
					<div class="col-sm-6">
						<?php if($categoryInfo['category_article_cover']): ?>
							<a href="javascript:;" id="upload-img">
								<img class="fit-cover" src="<?=$categoryInfo['category_article_cover']?>" width="160" height="160" />
							</a>
						<?php else: ?>
							<a href="javascript:;" id="upload-img">
								<img class="fit-cover" src="<?=$staticAdminUrl?>assets/images/upload.png" width="160" height="160" />
							</a>
						<?php endif; ?>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right">文章内容：</label>
					<div class="col-sm-6">
						<textarea class="pd0" id="content" name="category_article_content" style="width: 100%;height: 400px"><?=urldecode($categoryInfo['category_article_content'])?></textarea>
					</div>
				</div>
			</div>
				
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info" type="button">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</form>
	</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<!-- 百度编辑器 -->
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.config.js"></script>
<script src="<?=$staticAdminUrl?>assets/ueditor/ueditor.all.js"></script>
<script type="text/javascript">
	var ue;

	$(function(){
		// 切换栏目类型
		$("[name=category_type]").change(function(){

			var categoryType = $(this).val();
			if(categoryType == <?=CMS_CATEGORY_ARTICLE?>) {
				show_article_type();
			} else {
				$("#category-article-div").addClass("hide");
			}
		})

		// 初始化编辑器
		ue = UE.getEditor('content');

		// 修改栏目信息
		<?php if($categoryInfo): ?>
			$("[name=category_recommend]").val("<?=$categoryInfo['category_recommend']?>");
			// 显示栏目类型
			$("[name=category_type]").val("<?=$categoryInfo['category_type']?>")
			<?php if($categoryInfo['category_type'] == CMS_CATEGORY_ARTICLE): ?>
				show_article_type();
			<?php endif; ?>
		<?php endif; ?>

		// 提交表单
		var form = $("#category-form");
		var submitUrl = "<?=admin_url('cms/save_category_action')?>";


		// 点击提交按钮
		$("button.btn-info").click(function(){
			$("[name=category_article_content]").val(ue.getContent());
			form.submit();
		})
		// 提交表单
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.href = "<?=admin_url('cms/category')?>"
			});
		})
	})

	// 显示文章类型
	function show_article_type(){
		$("#category-article-div").removeClass("hide");

		// 设置高度
		setTimeout(function(){
			ue.setHeight(400);
		}, 300)

		// 上传图片对象
		$("#upload-img").uploader({
			"server":  '<?=UP_FORM_URL?>',
			"authorization": "<?=$signature?>",
			"policy": "<?=$policy?>",
			"callback": function(response) {
				var imgFile = "<?=UP_URL?>" + response.url;
				$("#upload-img").find("img").attr("src", imgFile);
				$("[name=category_article_cover]").val(imgFile);
			}
		});
	}
</script>